package com.ssh.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ssh.entity.User;
import com.ssh.entity.vo.CategoryVo;
import com.ssh.entity.vo.CommodityVo;
import com.ssh.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	//   http://localhost:8080/ssh/user/login.do
	@RequestMapping(value="/login.do")
	public String userLogin(String userName, String userPass, HttpServletRequest req){
		
		System.out.println("Hello SpringMVC~~~" + userName + "~~~" + userPass);
		User user = userService.loadUserByName(userName);
		if(null != user) {
			if(userPass.equals(user.getUserPass())) {
				req.getSession().setAttribute("loginUser", user);
				return "welcome";
			}else {
				req.setAttribute("loginError", "密码错误!");
			}
		}else {
			req.setAttribute("loginError", "用户名错误！");
		}
		return "login";
	}
	
	//  http://localhost:8080/ssh/user/reg.do
	@RequestMapping(value="/reg.do",method=RequestMethod.GET)
	public String userReg(){
		return null;
	}
	
	
	
	
	
	
	/**
	 * 前台用户首页展示商品栏目
	 * @return
	 */
	@RequestMapping(value="/homePageLoadCategorys.do")
	public String homePageLoadCategorys(HttpSession session){
		
		List<CategoryVo> volist1 = userService.findCategorysByParentid(-1);
		for(CategoryVo cvo : volist1){
			List<CategoryVo> volist2 = userService.findCategorysByParentid(cvo.getId());
			cvo.setChildren(volist2);
			for(CategoryVo cvo2 : volist2){
				List<CategoryVo> volist3 = userService.findCategorysByParentid(cvo2.getId());
				cvo2.setChildren(volist3);
			}
		}
		
		session.setAttribute("volist1", volist1);
		return "/lexian/home/home";
	}
	public String showListLoadCommoditys(){
		
		//List<CommodityVo> volist1 = userService.findCommoditysByC3id();
		
		
		
		
		
		
		
		
		
		
		
		
		return "/lexian/home/search";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
